package com.mjk.common.core.jdbc.ibatis.typehandler.handler;

import com.mjk.common.core.jdbc.ibatis.typehandler.BaseHandler;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.TimeUtils;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * 带时分秒的时间 格式为yyyy-MM-dd HH:mm:ss
 */
public class LocalDateTimeHandler implements BaseHandler<LocalDateTime> {
    @Override
    public String resovleToString(Field f, LocalDateTime localDateTime) {
        if (ObjectUtils.isEmpty(localDateTime)) {
            return null;
        }
        return localDateTime.format(DateTimeFormatter.ofPattern(TimeUtils.DATE_FORMAT_DATETIME));
    }

    @Override
    public LocalDateTime resovleResult(Object o) {
        if (o instanceof LocalDateTime localDateTime) {
            return localDateTime;
        }
        return LocalDateTime.ofInstant(TimeUtils.parse(o.toString()).toInstant(), ZoneId.systemDefault());
    }
}
